package cn.wolfcode.web.controller.audit;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.wolfcode.common.annotation.Log;
import cn.wolfcode.common.core.controller.BaseController;
import cn.wolfcode.common.core.domain.AjaxResult;
import cn.wolfcode.common.enums.BusinessType;
import cn.wolfcode.audit.domain.AudBpmnInfo;
import cn.wolfcode.audit.service.IAudBpmnInfoService;
import cn.wolfcode.common.utils.poi.ExcelUtil;
import cn.wolfcode.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 流程定义Controller
 * 
 * @author zhangyf
 * @date 2023-04-16
 */
@RestController
@RequestMapping("/audit/bpmnInfo")
public class AudBpmnInfoController extends BaseController
{
    @Autowired
    private IAudBpmnInfoService audBpmnInfoService;

    /**
     * 查询流程定义列表
     */
    @PreAuthorize("@ss.hasPermi('audit:bpmnInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(AudBpmnInfo audBpmnInfo)
    {
        startPage();
        List<AudBpmnInfo> list = audBpmnInfoService.selectAudBpmnInfoList(audBpmnInfo);
        return getDataTable(list);
    }

    /**
     * 导出流程定义列表
     */
    @PreAuthorize("@ss.hasPermi('audit:bpmnInfo:export')")
    @Log(title = "流程定义", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AudBpmnInfo audBpmnInfo)
    {
        List<AudBpmnInfo> list = audBpmnInfoService.selectAudBpmnInfoList(audBpmnInfo);
        ExcelUtil<AudBpmnInfo> util = new ExcelUtil<AudBpmnInfo>(AudBpmnInfo.class);
        util.exportExcel(response, list, "流程定义数据");
    }

    /**
     * 获取流程定义详细信息
     */
    @PreAuthorize("@ss.hasPermi('audit:bpmnInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(audBpmnInfoService.selectAudBpmnInfoById(id));
    }

    /**
     *新增流程定义
     *
     * file: （二进制）
     * bpmnLabel: doutang.jpg
     * bpmnType: 0
     * info: dd
     *
     */
    @PreAuthorize("@ss.hasPermi('audit:bpmnInfo:add')")
    @Log(title = "流程定义", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(AudBpmnInfo audBpmnInfo, MultipartFile file) throws IOException {
        return toAjax( audBpmnInfoService.saveBpmnInfo(audBpmnInfo,file.getInputStream()));
    }

    /**
     * 修改流程定义
     */
    @PreAuthorize("@ss.hasPermi('audit:bpmnInfo:edit')")
    @Log(title = "流程定义", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AudBpmnInfo audBpmnInfo)
    {
        return toAjax(audBpmnInfoService.updateAudBpmnInfo(audBpmnInfo));
    }

    /**
     * 删除流程定义
     */
    @PreAuthorize("@ss.hasPermi('audit:bpmnInfo:remove')")
    @Log(title = "流程定义", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(audBpmnInfoService.deleteAudBpmnInfoById(id));
    }

    /**
     *
     * 查看流程定义文件和图，响应文件内容
     */
    // 看需求，根据需求需要响应内容来决定处理方法怎么做？
    // 方法写void，由我们开发者来自定义响应内容。响应文件内容，常用于文件下载
    @GetMapping("/queryProcess/{id}/{type}")
    public void queryProcess(@PathVariable("id") Long id,@PathVariable("type")String type,HttpServletResponse response) throws IOException {
        InputStream in = audBpmnInfoService.getInputStream(id,type);  // 把文件内容或者图拿到封装一个 InputStream对象
        // response.getOutputStream():响应到浏览器
        FileCopyUtils.copy(in,response.getOutputStream());
    }

    /*public void a(HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.write("HTML内容");

        response.setContentType("application/json;charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.write("JSON内容");

        response.getOutputStream().write("".getBytes());
    }*/
}
